// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.audio.parameters;

import java.util.ArrayList;
import zombie.audio.FMODGlobalParameter;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoPlayer;
import zombie.core.math.PZMath;
import zombie.iso.IsoMetaGrid;
import zombie.iso.IsoWorld;

public final class ParameterZone extends FMODGlobalParameter {
    private final String m_zoneName;
    private final ArrayList<IsoMetaGrid.Zone> m_zones = new ArrayList();

    public ParameterZone(String name, String zoneName) {
        super(name);
        this.m_zoneName = zoneName;
    }

    @Override
    public float calculateCurrentValue() {
        IsoGameCharacter isoGameCharacter = this.getCharacter();
        if (isoGameCharacter == null) {
            return 40.0F;
        } else {
            byte _byte = 0;
            this.m_zones.clear();
            IsoWorld.instance.MetaGrid.getZonesIntersecting((int)isoGameCharacter.x - 40, (int)isoGameCharacter.y - 40, _byte, 80, 80, this.m_zones);
            float float0 = Float.MAX_VALUE;

            for (int _int = 0; _int < this.m_zones.size(); _int++) {
                IsoMetaGrid.Zone zone = (IsoMetaGrid.Zone)this.m_zones.get(_int);
                if (this.m_zoneName.equalsIgnoreCase(zone.getType())) {
                    if (zone.contains((int)isoGameCharacter.x, (int)isoGameCharacter.y, _byte)) {
                        return 0.0F;
                    }

                    float float1 = (float)zone.x + (float)zone.w / 2.0F;
                    float float2 = (float)zone.y + (float)zone.h / 2.0F;
                    float float3 = PZMath.max(PZMath.abs(isoGameCharacter.x - float1) - (float)zone.w / 2.0F, 0.0F);
                    float float4 = PZMath.max(PZMath.abs(isoGameCharacter.y - float2) - (float)zone.h / 2.0F, 0.0F);
                    float0 = PZMath.min(float0, float3 * float3 + float4 * float4);
                }
            }

            return (float)((int)PZMath.clamp(PZMath.sqrt(float0), 0.0F, 40.0F));
        }
    }

    private IsoGameCharacter getCharacter() {
        IsoPlayer isoPlayer0 = null;

        for (int _int = 0; _int < IsoPlayer.numPlayers; _int++) {
            IsoPlayer isoPlayer1 = IsoPlayer.players[_int];
            if (isoPlayer1 != null
                && (isoPlayer0 == null || isoPlayer0.isDead() && isoPlayer1.isAlive() || isoPlayer0.Traits.Deaf.isSet() && !isoPlayer1.Traits.Deaf.isSet())) {
                isoPlayer0 = isoPlayer1;
            }
        }

        return isoPlayer0;
    }
}
